package com.zzccyy.jpdemo.base

import android.R
import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.FragmentActivity
import com.sztle.admodule.apiprocessor.OutIntAdApi
import com.sztle.admodule.apiprocessor.OutNativeApi
import com.zzccyy.jpdemo.utils.CenterLoadingDialog
import com.zzccyy.jpdemo.utils.StatusBarUtil


/**
 * Created by ${chuan.qi} on 2018/7/27.
 * email:chuan.qi@ikang.com
 * explain:基类Activity
 */
abstract class BasicBaseActivity : FragmentActivity(), View.OnClickListener {
    private var centerLoadingDialog: CenterLoadingDialog? = null


    protected abstract val layoutId: Int
        get

    protected abstract fun findViews()

    protected abstract fun setListener()

    protected abstract fun initData()


    override fun onResume() {
        super.onResume()
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val layoutId = layoutId
        if (layoutId != 0) {
            setContentView(layoutId)
        }
        StatusBarUtil.setDarkMode(this)
        setFitsSystemWindows(this, false)
        StatusBarUtil.changStatusIconCollor(this, false)
        findViews()
        setListener()
        initData()

        if (isNeedAd()) {
            checkIntAd()
        }
        checkNativeAd()
    }

    open fun isNeedAd(): Boolean {
        return true
    }

    fun setMarginTop(view: View) {
        val params = view.layoutParams as LinearLayout.LayoutParams
        params.topMargin = params.topMargin + StatusBarUtil.getStatusBarHeight(applicationContext)
    }


    fun setFitsSystemWindows(activity: Activity, value: Boolean) {
        val contentFrameLayout = activity.findViewById<View>(R.id.content) as ViewGroup
        val parentView = contentFrameLayout.getChildAt(0)
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.fitsSystemWindows = value
        }
    }


    protected var oldId: Int = -1
    private var clickView: View? = null
    protected var lastClickTime: Long = 0L

    fun avoidFastClick(v: View) {
        if (oldId == -1) {
            clickView = v
            lastClickTime = System.currentTimeMillis()
            oldId = v.id
        } else if (v.id == oldId) {
            if (System.currentTimeMillis() - lastClickTime < SPACE_TIME) {
                v.id = 0
            } else {
                lastClickTime = System.currentTimeMillis()
            }
        } else if (v.id == 0) {
            if (System.currentTimeMillis() - lastClickTime > SPACE_TIME) {
                lastClickTime = System.currentTimeMillis()
                v.id = oldId
            }
        } else {
            lastClickTime = System.currentTimeMillis()
            clickView!!.id = oldId
            clickView = v
            oldId = v.id
        }
    }

    fun showLoading() {
        if (centerLoadingDialog == null) {
            centerLoadingDialog = CenterLoadingDialog(this)
        }
        if (!centerLoadingDialog!!.isShowing) {
            centerLoadingDialog!!.show()
        }
    }

    fun dissmissLoading() {
        if (centerLoadingDialog != null && centerLoadingDialog!!.isShowing) {
            centerLoadingDialog!!.dismiss()
        }
    }

    override fun onClick(v: View) {
        avoidFastClick(v)
    }


    companion object {
        protected const val SPACE_TIME: Int = 600
    }

    protected fun checkIntAd() {
        OutIntAdApi.showSingleIntAd(this, localClassName.toLowerCase().replace("activity", ""))
    }

    protected fun checkNativeAd() {
        if (getNativeView() == null) {
            return
        }
        OutNativeApi.showNativeAd(
            this,
            getNativeView(),
            localClassName.toLowerCase().replace("activity", "")
        )
    }

    fun getNativeView(): FrameLayout? {
        return null
    }
}
